<%@page import="java.sql.PreparedStatement"%>
<%@page import="Conexao.Conexao"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>WebRunners</title>
        <meta name="viewport" content="width=device-width; initial-scale=1.0">
        <link href="CSS/Reset.css" type="text/css" rel="stylesheet"/>
        <link href="CSS/Estilos.css" type="text/css" rel="stylesheet"/>
        <script>
            function mostra(id) {
                document.getElementById('cadastro').style.display = 'none';
                document.getElementById(id).style.display = 'block';
            }
        </script>
    </head>
    <body>
        <div class="login">
            <img src="Imagens/logo1.png"/>
            <form action="sessao.jsp" method="post">
                <table>
                    <tr><td><label>E-mail: <input type="text" class="campo" name="email" maxlength="100" required/></label></td></tr>
                    <tr><td><label>Senha: <input type="password" class="campo" name="senha" maxlength="20" required/></label></td></tr>
                    <tr><td><input type="submit" value="Logar" class="enviar"/></td></tr>
                    <tr><td><a onclick="mostra('cadastro')">Primeiro Acesso? Clique aqui.</a></td></tr>
                </table>
            </form>
        </div>
        <%
            session = request.getSession();
            if (request.getParameter("nome") != null) {
                Connection con = null;
                try {
                    con = Conexao.getConnection();
                    String nome = request.getParameter("nome");
                    String sobrenome = request.getParameter("sobrenome");
                    int altura = Integer.parseInt(request.getParameter("altura"));
                    int peso = Integer.parseInt(request.getParameter("peso"));
                    int idade = Integer.parseInt(request.getParameter("idade"));
                    String email = request.getParameter("email");
                    String senha = request.getParameter("senha");
                    PreparedStatement ps = con.prepareStatement("INSERT INTO USUARIO VALUES (?,?,?,?,?,?,?)");
                    ps.setString(1, nome);
                    ps.setString(2, sobrenome);
                    ps.setInt(3, altura);
                    ps.setInt(4, idade);
                    ps.setInt(5, peso);
                    ps.setString(6, email);
                    ps.setString(7, senha);
                    ps.execute();
                    ps.close();
                    out.println("<script type='text/javascript'>alert('Cadastro efetuado com sucesso');</script>");
                } catch (Exception e) {
                } finally {
                    con.close();
                }
            } else if (null != session.getAttribute("email")) {
                response.sendRedirect(request.getContextPath() + "/inicio.jsp");
            }
        %>
        <div class="cadastro" id="cadastro" style="display: none">
            <form action="#" method="post">
                <table>
                    <tr>
                        <th colspan="2">Crie sua conta</th>
                    </tr>
                    <tr>
                        <td>Nome:</td>
                        <td><input type="text" class="campo" name="nome" maxlength="20" required/></td>
                    </tr>	
                    <tr>
                        <td>Sobrenome:</td>
                        <td><input type="text" class="campo" name="sobrenome" maxlength="100" required/></td>
                    </tr>
                    <tr>
                        <td>Altura:</td>
                        <td><input type="number" class="campo" name="altura" required/></td>
                    </tr>
                    <tr>
                        <td>Peso:</td>
                        <td><input type="number" class="campo" name="peso" required/></td>
                    </tr>
                    <tr>
                        <td>Idade:</td>
                        <td><input type="number" class="campo" name="idade" required/></td>
                    </tr>
                    <tr>
                        <td>E-mail:</td>
                        <td><input type="email" class="campo" name="email" maxlength="100" required/></td>
                    </tr>
                    <tr>
                        <td>Senha:</td>
                        <td><input type="password" class="campo" name="senha" maxlength="20" required/></td>
                    </tr>	
                    <tr><td colspan="2"><input type="submit" value="Cadastrar" class="cadastrar"/></td></tr>			
                </table>								
            </form>			
        </div>
    </body>
</html>
